package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.*;
import cc.rengu.igas.momp.facade.bean.McOrgInfoBean;
import cc.rengu.igas.momp.facade.request.EditMchntFeeDiscountMcDetailRequest;
import cc.rengu.igas.momp.facade.request.ManageMchntFeeDiscountMcRequest;
import cc.rengu.igas.momp.facade.response.ManageMchntFeeDiscountMcResponse;
import cc.rengu.jrbdp.register.realtime.client.RealTimeRegisterClient;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户手续费优惠类活动信息管理功能实现
 *
 * @author Jinan Liu
 * @date 2020/4/2 17:45
 */
public class ManageMchntFeeDiscountMcService extends RadpService {

    private String globalInstId;
    private String globalMcNo;
    private String globalTimeStamp;
    private String globalOprId;
    private String globalTxnDate;
    private String globalTableName = TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode();
    /**
     * 批量任务注册参数类型 PARAM_TYPE
     */
    private static final String MOMP_RBDP_ADDRESS = "MOMP_RBDP_ADDRESS";
    /**
     * 批量任务注册地址参数键值 PARAM_KEY
     */
    private static final String MOMP_RBDP_REGISTER_ADDRESS = "MOMP_RBDP_REGISTER_ADDRESS";
    private String flow;
    private String relationMcNo;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest = new ManageMchntFeeDiscountMcRequest();
            ConvertUtil.convertOutput(manageMchntFeeDiscountMcRequest);

            ManageMchntFeeDiscountMcResponse manageMchntFeeDiscountMcResponse = new ManageMchntFeeDiscountMcResponse();
            manageMchntFeeDiscountMcResponse.setHeader(manageMchntFeeDiscountMcRequest.getHeader());
            BizResponse<ManageMchntFeeDiscountMcResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(manageMchntFeeDiscountMcResponse);

            String queryType = manageMchntFeeDiscountMcRequest.getQueryType();
            String queryRole = manageMchntFeeDiscountMcRequest.getQueryRole();
            String oprType = manageMchntFeeDiscountMcRequest.getOprType();
            globalInstId = manageMchntFeeDiscountMcRequest.getHeader().getInstId();
            globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalOprId = manageMchntFeeDiscountMcRequest.getQueryId();
            globalMcNo = manageMchntFeeDiscountMcRequest.getMcNo();
            globalTxnDate = manageMchntFeeDiscountMcRequest.getHeader().getTransDate();

            rglog.debug("QUERY_TYPE=<{}>", queryType);
            rglog.debug("QUERY_ROLE=<{}>", queryRole);
            rglog.debug("OPR_TYPE=<{}>", oprType);

            /* 报文检查 */
            CommonMessageCheck.checkBizMessageHeader(queryType, globalOprId, queryRole, rglog);
            CommonMessageCheck.checkSingleValue(oprType, rglog);
//            if (oprType.equals(CommonConstant.ONE_COMMON_CONSTANT)){
//                //判断未上送商户号商圈号时，机构下的非特殊费率商户达标统计表是否存该交易日期内的数据
//                checkMchntNoAndAreaNo(xmlTreeUtil,manageMchntFeeDiscountMcRequest);
//            }
            // 服务调用
            BizResponse<ManageMchntFeeDiscountMcResponse> bizResponseNew = manageMchntFeeDiscountMc(
                    manageMchntFeeDiscountMcRequest, manageMchntFeeDiscountMcResponse, xmlTreeUtil);
            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());

            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 减免类活动管理
     *
     * @param manageMchntFeeDiscountMcRequest  减免类营销活动管理接口请求对象
     * @param manageMchntFeeDiscountMcResponse 减免类营销活动管理接口应答对象
     * @param xmlTreeUtil                      内部XML树
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.ManageMchntFeeDiscountMcResponse> 减免类营销活动管理接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/29 01:38
     */
    private BizResponse<ManageMchntFeeDiscountMcResponse> manageMchntFeeDiscountMc(ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest, ManageMchntFeeDiscountMcResponse manageMchntFeeDiscountMcResponse, XmlTreeUtil xmlTreeUtil)
            throws Exception {

        BizResponse<ManageMchntFeeDiscountMcResponse> bizResponse = new BizResponse<>();

        /* 判断查询类型 */
        String queryType = manageMchntFeeDiscountMcRequest.getQueryType();
        String queryRole = manageMchntFeeDiscountMcRequest.getQueryRole();
        String oprType = manageMchntFeeDiscountMcRequest.getOprType();

        /* 判断用户的角色和权限 */
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(queryType)) {
            if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(queryRole)) {
                /* 银行审批岗-正式表和临时表所有数据 */
                if (OprTypeEnum.AUDIT_PASSED.getOprTypeCode().equals(oprType)) {
                    /* 审批通过 */
                    auditPassed(manageMchntFeeDiscountMcRequest);

                } else if (OprTypeEnum.AUDIT_REFUSED.getOprTypeCode().equals(oprType)) {
                    /* 审批拒绝 */
                    auditRefused(manageMchntFeeDiscountMcRequest);

                } else {
                    rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
                    throw new BizException(RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc());
                }

            } else if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(queryRole)) {
                if (OprTypeEnum.AUDIT_DELETE.getOprTypeCode().equals(oprType)) {
                    /* 删除草稿 */
                    deleteDraftMcData(manageMchntFeeDiscountMcRequest);
                }
            } else {
                /* 银行录入岗或银行发布岗 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
            }
        } else {
            /* 商户、代理商、收银员或用户 */
            rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
        }


        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        manageMchntFeeDiscountMcResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        manageMchntFeeDiscountMcResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(manageMchntFeeDiscountMcResponse);

        return bizResponse;
    }

    /**
     * 删除草稿
     *
     * @param manageMchntFeeDiscountMcRequest 减免类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void deleteDraftMcData(ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageMchntFeeDiscountMcRequest.getHeader().getInstId();
        String mcNo = manageMchntFeeDiscountMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        /* 查询营销活动信息 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = mcDetailInfoMapper.selectMchntFeeDiscountMcDetailInfo(instId, mcNo);
        if (null == tmMcMchntFeeDiscountInfo) {
            /* 获取营销活动信息失败 */
            rglog.error("获取营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc());
        }

        if (!McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMchntFeeDiscountInfo.getMcStatus())) {
            /* 营销活动状态异常，不可删除草稿 */
            rglog.error("{}}, MC_STATUS=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc(), tmMcMchntFeeDiscountInfo.getMcStatus(), mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc());
        }

        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMchntFeeDiscountInfo.getAuditStatus()) ||
                AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMchntFeeDiscountInfo.getAuditStatus()) ||
                AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMchntFeeDiscountInfo.getAuditStatus())) {
            /* 审批中不可删除草稿 */
            rglog.error("{}}, MC_STATUS=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc(), tmMcMchntFeeDiscountInfo.getMcStatus(), mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 删除营销活动信息 */
        returnCode = mcDetailInfoMapper.deleteMfdMcInfo(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动机构信息 */
        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        returnCode = mcOrgInfoMapper.deleteMcOrgInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动商户信息 */
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        returnCode = mcMchntInfoMapper.deleteMcMchntInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动规则信息 */
        McRuleInfoMapper mcRuleInfoMapper = new McRuleInfoMapperImpl();
        returnCode = mcRuleInfoMapper.deleteMfdRuleInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动产品信息 */
        McProductInfoMapper mcProductInfoMapper = new McProductInfoMapperImpl();
        returnCode = mcProductInfoMapper.deleteMcProductInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_PRODUCT_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_PRODUCT_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_PRODUCT_DATA_ERROR.getRespDesc());
        }

        /* 删除临时表营销活动所有信息 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        returnCode = platBizTmpMapper.deleteTmpMcDataByMcNo(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespDesc());
        }

        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 审批通过
     *
     * @param manageMchntFeeDiscountMcRequest 减免类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void auditPassed(ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageMchntFeeDiscountMcRequest.getHeader().getInstId();
        String mcNo = manageMchntFeeDiscountMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        rglog.debug("INST_ID=<{}>, MC_NO=<{}>", instId, mcNo);

        /* 营销活动信息正式表查询 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, mcNo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
        if (null == tmMcMchntFeeDiscountInfo) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, instId, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
        flow = tmMcMchntFeeDiscountInfo.getFlow();
        relationMcNo = tmMcMchntFeeDiscountInfo.getRelationMcNo();
        /* 营销活动信息临时表查询 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(instId, mcNo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
        if (null == platBizTmp) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 判断如何更新临时表数据 */
        String bizStatus = platBizTmp.getBizStatus();
        if (BizStatusEnum.TMP_STATUS.getBizStatusCode().equals(bizStatus)) {
            /* 非正式数据要更新为正式数据 */
            bizStatus = BizStatusEnum.NORMAL_STATUS.getBizStatusCode();
        }

        /* 根据现有营销活动状态判断如更新营销活动状态 */
        String mcStatus;
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(platBizTmp.getMcStatus())) {
            /* 当前营销活动状态是草稿状态,需要更新营销活动状态为未发布 */
            mcStatus = McStatusEnum.NOT_PUBLISHED.getMcStatusCode();
        } else {
            /* 当前营销活动状态不是草稿状态,不需要更新营销活动状态 */
            mcStatus = platBizTmp.getMcStatus();
        }

        /* 根据现有审批状态决定如何更新临时表数据 */
        String oldAuditStatus;
        String newAuditStatus;
        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {

            oldAuditStatus = AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode();
            newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

        } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {

            oldAuditStatus = AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode();
            newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

        } else {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
        }

        /* 更新临时表数据 */
        returnCode = platBizTmpMapper.updateTmpMcAllStatusByMcNoAndAuditStatus(instId, mcNo, bizStatus, mcStatus, newAuditStatus, oldAuditStatus, globalOprId, globalTimeStamp);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 判断如何更新正式表数据,营销活动状态是草稿或未发布状态才需要操作正式表数据 */
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMchntFeeDiscountInfo.getMcStatus()) || McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(tmMcMchntFeeDiscountInfo.getMcStatus())) {

            if (McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMchntFeeDiscountInfo.getMcStatus())) {
                /* 如果营销活动状态是草稿状态,需要将草稿状态变更为未发布状态,同时将审批状态变更为发布待审批状态 */
                TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo1 = new TMMcMchntFeeDiscountInfo();
                tmMcMchntFeeDiscountInfo1.setInstId(instId);
                tmMcMchntFeeDiscountInfo1.setMcNo(mcNo);
                tmMcMchntFeeDiscountInfo1.setMcStatus(McStatusEnum.NOT_PUBLISHED.getMcStatusCode());
                tmMcMchntFeeDiscountInfo1.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
                tmMcMchntFeeDiscountInfo1.setLastOprId(globalOprId);
                tmMcMchntFeeDiscountInfo1.setUpdateTime(globalTimeStamp);
                tmMcMchntFeeDiscountInfo1.setAuditRefuseReason(manageMchntFeeDiscountMcRequest.getOprContent());

                returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMchntFeeDiscountInfo1, globalTableName);
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }
            } else {
                /* 如果营销活动状态是未发布状态,需要将审批状态变更为发布待审批状态 */
                if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMchntFeeDiscountInfo.getAuditStatus())) {
                    /* 新增审批流程 */
                    oldAuditStatus = AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode();
                    newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

                } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMchntFeeDiscountInfo.getAuditStatus())) {
                    /* 修改审批审批流程 */
                    oldAuditStatus = AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode();
                    newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

                } else {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
                }
                /* 更新正式表数据 */
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode(), instId, mcNo, newAuditStatus, oldAuditStatus, globalOprId, globalTimeStamp, tmMcMchntFeeDiscountInfo.getAuditRefuseReason());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }
            }
            if (!tmMcMchntFeeDiscountInfo.getStockMchntFlag().equals(CommonConstant.STOCK_MCHNT_FLAG_ZERO)) {
                //注册定时任务
                timingTask(CommonConstant.MFD_TIME_TASK_NAME, globalTxnDate, "", tmMcMchntFeeDiscountInfo.getInstId(), tmMcMchntFeeDiscountInfo.getMcNo());

            }
        } else if (McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMchntFeeDiscountInfo.getMcStatus()) &&
                platBizTmp.getBizStatus().equals(BizStatusEnum.TMP_STATUS.getBizStatusCode()) &&
                platBizTmp.getMcStatus().equals(McStatusEnum.DRAFT.getMcStatusCode()) &&
                (platBizTmp.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode()) || platBizTmp.getAuditStatus().equals(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode()))) {
            int returnCodes = mcDetailInfoMapper.updateMcAllStatusByMcNoAndOriStatusAndTableName(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode(), instId, mcNo, McStatusEnum.PROGRESSING.getMcStatusCode(), McStatusEnum.PROGRESSING.getMcStatusCode(),
                    oldAuditStatus, AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
            if (returnCodes != 0) {
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
            }
            //更新正式表
            TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo1 = new TMMcMchntFeeDiscountInfo();
            tmMcMchntFeeDiscountInfo1.setInstId(instId);
            tmMcMchntFeeDiscountInfo1.setMcNo(mcNo);
            tmMcMchntFeeDiscountInfo1.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            tmMcMchntFeeDiscountInfo1.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
            tmMcMchntFeeDiscountInfo1.setLastOprId(globalOprId);
            tmMcMchntFeeDiscountInfo1.setUpdateTime(globalTimeStamp);

            returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMchntFeeDiscountInfo1, globalTableName);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        } else {
            /* 营销活动状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
        }

        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 审批拒绝
     *
     * @param manageMchntFeeDiscountMcRequest 减免类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void auditRefused(ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageMchntFeeDiscountMcRequest.getHeader().getInstId();
        String mcNo = manageMchntFeeDiscountMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        /* 营销活动信息正式表查询 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcMchntFeeDiscountInfo tmMcMoneyOffInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, mcNo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
        if (null == tmMcMoneyOffInfo) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, instId, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 营销活动信息临时表查询 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(instId, mcNo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
        if (null == platBizTmp) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, tmMcMoneyOffInfo.getMcNo());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 判断如何更新临时表数据 */
        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            returnCode = platBizTmpMapper.updateTmpMcAuditStatusByMcNoAndAuditStatus(instId, mcNo,
                    AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            returnCode = platBizTmpMapper.updateTmpMcAuditStatusByMcNoAndAuditStatus(instId, mcNo,
                    AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else if ((AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) || AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()))
                && McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MFD_INFO.getTableNameDesc());
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode());
                returnCode = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode());
                returnCode = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            }
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
            int returnCodes = 0;
            tmPlatBizTmp.setInstId(globalInstId);
            tmPlatBizTmp.setMcNo(mcNo);
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode());
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameDesc());
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode());
                returnCodes = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode());
                returnCodes = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            }
            if (Database.DBS_SUCCESS != returnCodes && Database.DBS_NOT_FOUND != returnCodes) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        } else {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
        }

        /* 判断如何更新正式表数据 */
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) || McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMoneyOffInfo.getAuditStatus())) {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMchntFeeDiscountMcRequest.getOprContent());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }

            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMoneyOffInfo.getAuditStatus())) {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMchntFeeDiscountMcRequest.getOprContent());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }

            } else {
                /* 营销活动审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
            }

        } else if ((AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) || AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()))
                && McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            //进行中商户增加审批拒绝  正式表改为新增拒绝状态
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMchntFeeDiscountMcRequest.getOprContent());
            } else {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMchntFeeDiscountMcRequest.getOprContent());
            }
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
            //查询营销商户表数据进行中新增商户数据不是转移商户数据
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            List<TMMcMchntInfo> tmMcMchntInfoList = mcMchntInfoMapper.selectMcMchntInfoListByinstIdMcMoFlag(tmMcMoneyOffInfo.getInstId(), tmMcMoneyOffInfo.getMcNo(), CommonConstant.MC_EXIT_FLAG, CommonConstant.MCHNT_NEW_FLAG);
            if (null != tmMcMchntInfoList && tmMcMchntInfoList.size() > 0) {
                for (TMMcMchntInfo tmMcMchntInfo : tmMcMchntInfoList) {
                    tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_REFUSE_FLAG);
                }
                //进行中新增商户拒绝修改状态 newMchntFlag 改为2
                returnCode = mcMchntInfoMapper.updateMcMchntInfoListByBank(tmMcMchntInfoList);
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{},营销活动商户正式表数据更新失败>", RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                }
            }
        } else {
            /* 营销活动状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
        }


        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 将临时表新增商户数据插入正式表
     *
     * @param tmMcMchntInfoList 营销活动商户信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/26 00:16
     */
    private int insertMcMchntInfoListByBank(List<TMMcMchntInfo> tmMcMchntInfoList) throws Exception {

        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        return mcMchntInfoMapper.insertMcMchntInfoListByBank(tmMcMchntInfoList);
    }

    /**
     * 取待提交发布营销活动商户规则临时表中待提交发布的商户数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMPlatBizTmp> 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/25 23:07
     */
    private List<TMPlatBizTmp> selectNewAddedMchntInfoForPublishing(TMPlatBizTmp tmPlatBizTmp) throws Exception {

        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectNewAddedMchntInfoForPublishing(tmPlatBizTmp);
    }

    /**
     * 判断未上送商户号商圈号时，机构下的非特殊费率商户达标统计表是否存该交易日期内的数据
     *
     * @param manageMchntFeeDiscountMcRequest
     * @author JL Pang
     */
    private void checkMchntNoAndAreaNo(XmlTreeUtil xmlTreeUtil, ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest) throws Exception {
        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        //查询该营销活动基本信息
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        MfdMcListInfoInObj mfdMcListInfoInObj = new MfdMcListInfoInObj();
        mfdMcListInfoInObj.setInstId(globalInstId);
        mfdMcListInfoInObj.setMcNo(globalMcNo);
        mfdMcListInfoInObj.setOrgId(manageMchntFeeDiscountMcRequest.getQueryOrgId());
        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = mcDetailInfoMapper.selectMfdMcDetailInfoByBankBasicRole(mfdMcListInfoInObj);
        //如果营销活动不是进行中状态
        if (!tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.PROGRESSING.getMcStatusCode())) {
            /* 开启数据库事务 */
            String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
            }
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            dbsUtil.dbsBeginTransaction();
            //获取当前时间
            String date = DateUtil.getCurrentDate();
            //查询营销活动商户信息
            List<TMMcMchntInfo> tmMcMchntInfoList = mcMchntInfoMapper.selectMcMchntInfoList(globalInstId, globalMcNo);
            if (tmMcMchntFeeDiscountInfo.getStockMchntFlag().equals(CommonConstant.STOCK_MCHNT_FLAG_ONE)) {
                if (null != tmMcMchntInfoList && tmMcMchntInfoList.size() > 0) {
                    for (TMMcMchntInfo tmMcMchntInfo : tmMcMchntInfoList) {
                        //查询手续费达标统计表是否有该商户现交易日期达标的数据
                        TMStatMfdCriteriaMapper tmStatMfdCriteriaMapper = new TMStatMfdCriteriaMapperImpl();
                        TMStatMfdCriteria tmStatMfdCriteria = tmStatMfdCriteriaMapper.selectTMStatMfdCriteriaByUsingMchntNoAndDateRange(globalInstId, tmMcMchntInfo.getMchntNo(), date);
                        //如果达标统计表有当前交易日期商户达标的数据，则直接拒绝新增,原活动状态为自然结束，强制结束，已结束的数据作废
                        if (null != tmStatMfdCriteria) {
                            /* 营销活动信息正式表查询 */
                            TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo1 = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmStatMfdCriteria.getMcNo(), globalTableName);
                            if (null != tmStatMfdCriteria && (null == tmMcMchntFeeDiscountInfo1 || tmMcMchntFeeDiscountInfo1.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) || tmMcMchntFeeDiscountInfo1.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) || tmMcMchntFeeDiscountInfo1.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode()))) {
                                rglog.error("交易流水号：<{}>, 商户<{}>已参加其他手续费活动！", manageMchntFeeDiscountMcRequest.getHeader().getTraceNo(), tmMcMchntInfo.getMchntNo());
                                throw new BizException(RespCodeEnum.WRONG_MCHNT_ISALREADY_ORTHER_INMC_ERROR.getRespCode(), RespCodeEnum.WRONG_MCHNT_ISALREADY_ORTHER_INMC_ERROR.getRespDesc());
                            }
                        }
                    }
                }

            } else if (tmMcMchntFeeDiscountInfo.getStockMchntFlag().equals(CommonConstant.STOCK_MCHNT_FLAG_TWO)) {
                //查询营销活动机构信息
                List<TMMcOrgInfo> orgInfoList = mcOrgInfoMapper.selectMcOrgInfoList(globalInstId, globalMcNo);
                OrganizationMapper organizationMapper = new OrganizationMapperImpl();
                //处理接口请求上送的机构信息
                List<String> requestOrgIdList = orgInfoList.stream().map(item -> item.getOrgId()).collect(Collectors.toList());
                //对请求上送的机构进行去重处理
                requestOrgIdList = requestOrgIdList.stream().distinct().collect(Collectors.toList());

                //需要从接口上送的机构信息中移除的机构
                List<String> removeOrgIdList = new ArrayList<>();
                List<String> requestResultOrgList = new ArrayList<>();

                String queryOrgId = manageMchntFeeDiscountMcRequest.getQueryOrgId();

                //查询当前登录用户的所属机构信息
                Organization queryOrgInfo = organizationMapper.selectOrganization(globalInstId, queryOrgId);

                //当前登录用户所属机构的最大深度
                int orgLevealDepth = 5;
                while (orgLevealDepth > 0) {
                    if (null != queryOrgInfo) {
                        String superOrgId = queryOrgInfo.getSuperOrgCode();
                        String orgLevel = queryOrgInfo.getOrgLevel();
                        rglog.info("交易流水号：<{}>,当前机构机构编码<{}>,上级机构编码<{}>,机构编码级别<{}>", manageMchntFeeDiscountMcRequest.getHeader().getTraceNo(), queryOrgInfo.getOrgCode(), orgLevel);

                        if (null != superOrgId && !"00".equals(orgLevel)) {
                            removeOrgIdList.add(superOrgId);
                            rglog.info("交易流水号：<{}>,接口请求上送的机构<{}>需要去除!", manageMchntFeeDiscountMcRequest.getHeader().getTraceNo(), superOrgId);
                            queryOrgInfo = organizationMapper.selectOrganization(globalInstId, superOrgId);
                        } else {
                            break;
                        }
                        orgLevealDepth--;
                    } else {
                        rglog.error("交易流水号：<{}>, 机构<{}>机构信息查询失败！", manageMchntFeeDiscountMcRequest.getHeader().getTraceNo(), removeOrgIdList.size(), queryOrgId);
                        throw new BizException(RespCodeEnum.NO_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.NO_ORG_INFO_ERROR.getRespDesc());
                    }
                }

                if (null != removeOrgIdList && !removeOrgIdList.isEmpty()) {
                    rglog.info("交易流水号：<{}>, 接口上送的机构机构信息中有<{}>个不能参加活动（操作员所属机构的上属机构不能参加）", manageMchntFeeDiscountMcRequest.getHeader().getTraceNo(), removeOrgIdList.size());
                    requestResultOrgList = requestOrgIdList.stream().filter(item -> !removeOrgIdList.contains(item)).collect(Collectors.toList());
                } else {
                    requestResultOrgList = requestOrgIdList;
                }
                //上送机构下所有商户
                List<TBMchntBaseInfo> newAlltbMchntBaseInfoList = new ArrayList<>();
                //循环查询机构下所有商户信息
                for (int i = 0; i < requestResultOrgList.size(); i++) {
                    List<TBMchntBaseInfo> tbMchntBaseInfoList = mchntInfoMapper.selectMchntBaseInfoListBySignInstId(globalInstId, requestResultOrgList.get(i));
                    if (null != tbMchntBaseInfoList && tbMchntBaseInfoList.size() > 0) {
                        newAlltbMchntBaseInfoList.addAll(tbMchntBaseInfoList);
                    }
                }
                //不包含特殊费率商户的list
                List<TBMchntBaseInfo> newtbMchntBaseInfoList = new ArrayList<>();
                for (TBMchntBaseInfo tbMchntBaseInfo : newAlltbMchntBaseInfoList) {
                    //判断商户是否存在特殊费率
                    List<TBMchntFeeInfo> tbMchntFeeInfoList = mchntInfoMapper.selectMchntFeeInfoListByMchntNo(globalInstId, tbMchntBaseInfo.getMchntNo());
                    if (null != tbMchntFeeInfoList && tbMchntFeeInfoList.size() > 0) {
                        tbMchntFeeInfoList = tbMchntFeeInfoList.stream().filter(item -> item.getFeeInputMode().equals(CommonConstant.ZERO_COMMON_CONSTANT)).collect(Collectors.toList());
                        if (null == tbMchntFeeInfoList || tbMchntFeeInfoList.size() == 0) {
                            newtbMchntBaseInfoList.add(tbMchntBaseInfo);
                        } else {
                            continue;
                        }
                    }
                    //查询手续费达标统计表是否有该商户现交易日期达标的数据
                    TMStatMfdCriteriaMapper tmStatMfdCriteriaMapper = new TMStatMfdCriteriaMapperImpl();
                    TMStatMfdCriteria tmStatMfdCriteria = tmStatMfdCriteriaMapper.selectTMStatMfdCriteriaByUsingMchntNoAndDateRange(globalInstId, tbMchntBaseInfo.getMchntNo(), date);
                    //如果达标统计表有当前交易日期商户达标的数据，则直接拒绝新增,原活动状态为自然结束，强制结束，已结束的数据作废
                    if (null != tmStatMfdCriteria) {
                        /* 营销活动信息正式表查询 */
                        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo1 = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmStatMfdCriteria.getMcNo(), globalTableName);
                        if (null != tmStatMfdCriteria && (null == tmMcMchntFeeDiscountInfo1 || tmMcMchntFeeDiscountInfo1.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) || tmMcMchntFeeDiscountInfo1.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) || tmMcMchntFeeDiscountInfo1.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode()))) {
                            rglog.error("交易流水号：<{}>, 商户<{}>已参加其他手续费活动！", manageMchntFeeDiscountMcRequest.getHeader().getTraceNo(), tbMchntBaseInfo.getMchntNo());
                            throw new BizException(RespCodeEnum.WRONG_MCHNT_ISALREADY_ORTHER_INMC_ERROR.getRespCode(), RespCodeEnum.WRONG_MCHNT_ISALREADY_ORTHER_INMC_ERROR.getRespDesc());
                        }
                    }
                }
                //判断是否有新增达标的商户
                if (null != newtbMchntBaseInfoList && newtbMchntBaseInfoList.size() > 0) {
                    //比较营销活动商户表与机构下达标商户 取出未插入营销活动商户表的商户信息
                    for (TMMcMchntInfo tmMcMchntInfo : tmMcMchntInfoList) {
                        newtbMchntBaseInfoList = newtbMchntBaseInfoList.stream().filter(item -> !item.getMchntNo().equals(tmMcMchntInfo.getMchntNo())).collect(Collectors.toList());
                    }
                    //如果存在未插入营销活动商户表的商户信息，则插入营销活动商户表
                    if (null != newtbMchntBaseInfoList && newtbMchntBaseInfoList.size() > 0) {
                        List<TMMcMchntInfo> newtmMcMchntInfoList = newtbMchntBaseInfoList.stream().map(this::convertMcMchntInfoByBaseInfo).collect(Collectors.toList());
                        int returnCode = mcMchntInfoMapper.insertMcMchntInfoListByBank(tmMcMchntInfoList);
                        if (returnCode != Database.DBS_SUCCESS) {
                            /* 异常结束数据库事务 */
                            rglog.error("新增营销活动商户信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
                            dbsUtil.dbsEndTransaction(false);
                            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                        }
                    }
                }
            }
        }
    }

    /**
     * 营销活动商户信息数据库实体类和接口实体类数据转换
     *
     * @param tbMchntBaseInfo 商户基本信息表
     * @return cc.rengu.igas.momp.common.entity.TBMchntBaseInfo 商户基本信息表
     * @author JL Pang
     * @since 2020/4/1 21:10
     */
    private TMMcMchntInfo convertMcMchntInfoByBaseInfo(TBMchntBaseInfo tbMchntBaseInfo) {
        TMMcMchntInfo mcMchntInfo = new TMMcMchntInfo();
        try {
            /* 法人机构号 */
            mcMchntInfo.setInstId(globalInstId);
            /* 营销活动编号 */
            mcMchntInfo.setMcNo(globalMcNo);
            /* 商户范围类型 */
            mcMchntInfo.setMchntType(tbMchntBaseInfo.getMchntType());
            /* 商户范围号 */
            mcMchntInfo.setMchntNo(tbMchntBaseInfo.getMchntNo());
            /* 商户范围名称 */
            mcMchntInfo.setMchntName(tbMchntBaseInfo.getMchntName());
            /* 新增商户标志 */
            mcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
            /* 退出标志 */
            mcMchntInfo.setExitFlag(CommonConstant.MC_NOT_EXIT_FLAG);
            /* 达标日期 */
            mcMchntInfo.setQualifiedDate(globalTxnDate);
            /* 创建人 */
            mcMchntInfo.setOprId(globalOprId);
            /* 创建时间 */
            mcMchntInfo.setCreateTime(globalTimeStamp);

        } catch (Exception e) {
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcMchntInfo;
    }

    /**
     * 注册定时任务
     *
     * @param taskName   任务名称
     * @param settleDate 清算日期 不上送 默认当前日期
     * @param regTime    执行日期 不上送 立即执行
     * @param taskBatch  批次号后五位清算日期内唯一
     * @param taskParam  营销活动编号
     * @throws Exception
     */
    private void timingTask(String taskName, String settleDate, String regTime, String taskBatch, String taskParam) throws Exception {
        taskBatch = taskBatch.concat(MompToolUtil.getAppoint(MompDateUtil.getTime(), 0, 5));
        rglog.info("任务名称<{}> 清算日期<{}> 执行时间<{}> 批次号<{}> 营销活动编号<{}>", taskName, settleDate, regTime, taskBatch, taskParam);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, MOMP_RBDP_ADDRESS, MOMP_RBDP_REGISTER_ADDRESS);
        if (null == sysParam) {
            rglog.error("获取批量任务注册地址失败！");
            throw new BizException(RespCodeEnum.FAILED_TO_GET_RBDP_REGISTER_ADDRESS.getRespCode(), RespCodeEnum.FAILED_TO_GET_RBDP_REGISTER_ADDRESS.getRespDesc());
        }
        //批量注册地址
        String rbdpserver = sysParam.getParamValue();
        RealTimeRegisterClient a = RbdpClient.getIntance1(rglog, rbdpserver);

        int ret = a.registerTask(taskName, settleDate, settleDate.concat(DateUtil.getCurrentTime()), taskBatch, taskParam);
        rglog.info("实时任务注册返回：ret {}", ret);
        String result = "";
        if (0 != ret) {
            result = "注册失败: " + RbdpClient.getIntance1(rglog, rbdpserver).getError();
            rglog.error("实时任务注册返回：ret {}", result);
            throw new BizException(RespCodeEnum.FAILED_TO_TIME_TASK.getRespCode(), RespCodeEnum.FAILED_TO_TIME_TASK.getRespDesc());
        } else {
            result = "注册成功.";
            rglog.info("实时任务注册返回：ret {}", result);
        }
    }

    /*
     *检查商户是否参加其他银行对商户手续费减免活动
     * @author JL Pang
     * @since 2020/9/10 22:26
     *tmMcMchntInfoList 新增商户list
     */
    private void checkMchntStatus(List<TMMcMchntInfo> newtmMcMchntInfoList, DbsUtil dbsUtil) throws Exception {
        //查询营销商户表数据
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        for (TMMcMchntInfo newtmMcMchntInfo : newtmMcMchntInfoList) {
            List<TMMcMchntInfo> tmMcMchntInfoList = mcMchntInfoMapper.selectMfdMchntInfoListByinstIdMchntNo(globalInstId, newtmMcMchntInfo.getMchntNo());
            if (null != tmMcMchntInfoList && tmMcMchntInfoList.size() > 0) {
                //去除本次活动和要转移的活动编号
                tmMcMchntInfoList = tmMcMchntInfoList.stream().filter(item -> !item.getMcNo().equals(globalMcNo) && !item.getMcNo().equals(relationMcNo)).collect(Collectors.toList());
                //上送商户是否参加了当前日期内其他进行中的手续费营销活动(new_mchnt_flag为1 exit_flag为1) (new_mchnt_flag为0 exit_flag为0)
                List<TMMcMchntInfo> jxzMchntList = tmMcMchntInfoList.stream().filter(item -> (item.getNewMchntFlag().equals(CommonConstant.MCHNT_OLD_FLAG) && item.getExitFlag().equals(CommonConstant.MC_NOT_EXIT_FLAG)) || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_NEW_FLAG) && item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG))
                        || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_OLD_FLAG) && item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG))).collect(Collectors.toList());
                rglog.info("营销活动商户信息表状态为(new_mchnt_flag为1 exit_flag为1)或(new_mchnt_flag为0 exit_flag为0)或(new_mchnt_flag为0 exit_flag为1)的条数<{}>", jxzMchntList.size());
                if (jxzMchntList.size() > 0) {
                    for (TMMcMchntInfo tmMcMchntInfo : jxzMchntList) {
                        /* 营销活动信息正式表查询 */
                        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmMcMchntInfo.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                        //如果活动状态不是自然结束，强制结束，草稿；审批状态不是新增拒绝，审批拒绝，发布拒绝的状态，直接抛异常
                        if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())
                                && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode()) && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode()) && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.PUBLISH_AUDIT_REJECT.getAuditStatusCode())
                                && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.DRAFT.getMcStatusCode())) {
                            rglog.error("商户<{}>已参加其他进行中手续费活动<{}>！", tmMcMchntInfo.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                            dbsUtil.dbsEndTransaction(false);
                            throw new BizException(RespCodeEnum.WRONG_MCHNT_ISALREADY_INMC_ERROR.getRespCode(), RespCodeEnum.WRONG_MCHNT_ISALREADY_INMC_ERROR.getRespDesc() + ",商户<" + tmMcMchntInfo.getMchntNo() + ">");
                        }
                    }
                }
                //上送商户是否有从a活动到b活动的转移(new_mchnt_flag为2 exit_flag为2) (new_mchnt_flag为1 exit_flag为2)
                List<TMMcMchntInfo> zyMchntList = tmMcMchntInfoList.stream().filter(item -> (item.getNewMchntFlag().equals(CommonConstant.MCHNT_REFUSE_FLAG) && item.getExitFlag().equals(CommonConstant.MC_TRANSFER_FLAG)) || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_NEW_FLAG) && item.getExitFlag().equals(CommonConstant.MC_TRANSFER_FLAG))).collect(Collectors.toList());
                rglog.info("营销活动商户信息表状态为(new_mchnt_flag为2 exit_flag为2)或(new_mchnt_flag为1 exit_flag为2)的条数<{}>", zyMchntList.size());
                if (zyMchntList.size() > 0) {
                    for (TMMcMchntInfo tmMcMchntInfo : zyMchntList) {
                        /* 营销活动信息正式表查询 */
                        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(globalInstId, tmMcMchntInfo.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                        if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                            rglog.error("商户<{}>正在进行活动转移！", tmMcMchntInfo.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                            dbsUtil.dbsEndTransaction(false);
                            throw new BizException(RespCodeEnum.MCHNT_TRANSFER_WRONG.getRespCode(), RespCodeEnum.MCHNT_TRANSFER_WRONG.getRespDesc() + ",商户<" + tmMcMchntInfo.getMchntNo() + ">");
                        }
                    }
                }
            }
        }
    }
}

